package com.googlecode.toolkit.encoder;

import java.util.LinkedList;
import java.util.List;

/**
 * 多个编码器组合编码
 * 
 * @author xiayg
 *
 */
public class CompositeTextEncoder implements TextEncoder {

    /**
     * 文本编码器集合
     */
    private List<TextEncoder> textEncoders = new LinkedList<TextEncoder>();
    
    public CompositeTextEncoder() { }
    
    /*
     * (non-Javadoc)
     * @see com.googlecode.toolkit.encoder.TextEncoder#encode(java.lang.String)
     */
    @Override
    public String encode(String text) {
        if (text == null) {
            return null;
        }
        String textToUse = text;
        for (TextEncoder textEncoder : this.textEncoders) {
            textToUse = textEncoder.encode(textToUse);
        }
        return textToUse;
    }
    
    /**
     * 添加文本编码器
     * @param textEncoder
     * @return
     */
    public CompositeTextEncoder addTextEncoder(TextEncoder textEncoder) {
        if (textEncoder != null) {
            this.textEncoders.add(textEncoder);
        }
        return this;
    }

}
